<?php
namespace app\api\controller;

use app\api\controller\Common;
use app\api\model\Warehouse as WarehouseModel;
use think\facade\Request;
use think\Db;
/**
 *  库存流水
 * Class Turnover
 * @package app\api\controller\Warehouse\Inventory
 */
class WarehouseTurnover extends Common
{

    /**
     * @param Request $request
     * @param WarehouseModel\InventoryTurnover $turnoverModel
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function index(Request $request, WarehouseModel\InventoryTurnover $turnoverModel)
    {
        $data =$request::param();
        $where =[];
        if(!empty($data['warehouse'])){
            $where[] =['fw.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['color'])){
            $where[] =['p.color','=',$data['color']];
        }
        if(!empty($data['sizes'])){
            $where[] =['p.size','=',$data['sizes']];
        }
        if(!empty($data['start_time']) && !empty($data['end_time'])){
            $where[] =['fw.create_time','between',[strtotime($data['start_time']),strtotime($data['end_time'])]];
        }
        if(!empty($data['name'])){
            $where[] =['com.id','=',$data['name']];
        }
        $list =Db::name('flowing_water')->alias('fw')
            ->leftJoin('product p','p.id = fw.goods_id')
            ->leftJoin('commodity com','com.id = p.commodity_id')
            ->leftJoin('color3 c3','c3.id = p.color')
            ->leftJoin('color3 c4','c4.id = p.size')
            ->leftJoin('warehouse w','w.id = fw.warehouse')
            ->field('fw.id,warehouse,fw.create_time,type,number,add_num,sur_num,p.color,size,com.name,item,
                c3.name as color,c4.name as size,w.name as warehouse')
            ->where($where)->order('id desc')->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        foreach ($list['data'] as $k=>$v){
            $list['data'][$k]['create_time'] =date('Y-m-d',$v['create_time']);
        }
        return $this->returnJson(200,'ok',$list);
    }

    public function StockList(Request $request, WarehouseModel $warehouseModel)
    {
        $data =$request::param();
        $where =[];
        if(!empty($data['warehouse'])){
            $where[] =['ps.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['name'])){
            $where[] =['com.id','=',$data['name']];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['sizes'])){
            $where[] =['p.size','=',$data['sizes']];
        }
        if(!empty($data['filter'])){
            $where[] =['ps.stock','>',0];
        }
        if(isset($data['single'])){
            //单品列表
            $list =Db::name('product_stock')->alias('ps')
                ->leftJoin('product p','p.id = ps.product_id')
                ->leftJoin('commodity com','com.id = ps.commodity_id')
                ->leftJoin('color3 c3','c3.id = p.color')
                ->leftJoin('color3 c4','c4.id = p.size')
                ->leftJoin('color1 c1','c1.id = com.unit')
                ->leftJoin('category c','c.id = com.category')
                ->leftJoin('warehouse w','w.id = ps.warehouse')
                ->field('ps.stock,warehouse,p.id,color,size,stock_upper,stock_lower,single_code,single_number,com.name,item,goods_code,
                images,category,unit,c3.name as color,c4.name as size,c1.name as unit,c.name as category,w.name as warehouse')
                ->where($where)->order('id desc')->page($data['page'],$data['size'])->paginate($data['size']);
        }else{
            //商品列表
            $list =Db::name('product_stock')->alias('ps')
                ->leftJoin('product p','p.id = ps.product_id')
                ->leftJoin('commodity com','com.id = ps.commodity_id')
                ->leftJoin('color3 c3','c3.id = p.color')
                ->leftJoin('color3 c4','c4.id = p.size')
                ->leftJoin('color1 c1','c1.id = com.unit')
                ->leftJoin('category c','c.id = com.category')
                ->leftJoin('warehouse w','w.id = ps.warehouse')
                ->field('ps.stock,warehouse,p.id,color,size,stock_upper,stock_lower,com.name,item,goods_code,images,category,unit,
                c3.name as color,c4.name as size,c1.name as unit,c.name as category,w.name as warehouse')
                ->where($where)->order('id desc')->page($data['page'],$data['size'])->paginate($data['size']);
        }
        $list =$list->toArray();
        foreach ($list['data'] as $k=>$v){
            $list['data'][$k]['images'] =json_decode($v['images'],true);
        }
        return $this->returnJson(200,'ok',$list);
    }
}
